package hotLeetcode;

import java.util.Arrays;

public class H03Leetcode3 {
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        Arrays.sort(nums);
        int count = 0;
        int result = 0;
        int i = 0, j = 1;
        while (j < nums.length) {
            if (nums[i] + 1 == nums[j]) {
                count++;
            } else if (nums[i] == nums[j]) {
                i++;
                j++;
                continue;
            } else {
                count = 0;
            }
            j++;
            i++;
            result = Math.max(result, count);

        }
        return result + 1;
    }


    public static void main(String[] args) {
        int[] nums = {1,2,0,1};
        H03Leetcode3 h03Leetcode3 = new H03Leetcode3();
        System.out.println(h03Leetcode3.longestConsecutive(nums));

    }
}
